package com.rtx.edi.platform.rule.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.utils.MultipleDataSource;
import com.rtx.edi.platform.rule.bean.TargetFieldConvertRule;
import com.rtx.edi.platform.rule.mapper.TargetFieldConvertRuleMapper;
import com.rtx.edi.platform.rule.service.TargetFieldConvertRuleService;

@Service
public class TargetFieldConvertRuleServiceImpl extends ServiceImpl<TargetFieldConvertRuleMapper, TargetFieldConvertRule> implements TargetFieldConvertRuleService{

	@Override
	public void selectList(Page<TargetFieldConvertRule> pageParam, TargetFieldConvertRule po) {
        LambdaQueryWrapper<TargetFieldConvertRule> query = Wrappers.lambdaQuery();
        query.like(po.getName() != null,TargetFieldConvertRule::getName, po.getName());
        query.like(po.getDatasource() != null,TargetFieldConvertRule::getDatasource, po.getDatasource());
        query.like(po.getListname() != null,TargetFieldConvertRule::getListname, po.getListname());
        query.like(po.getTargetfield() != null,TargetFieldConvertRule::getTargetfield, po.getTargetfield());
        query.like(po.getErrornulltargetfield() != null,TargetFieldConvertRule::getErrornulltargetfield, po.getErrornulltargetfield());
//        query.like(po.getErrormultipletarget() != null,TargetFieldConvertRule::getErrormultipletarget, po.getErrormultipletarget());
        baseMapper.selectPage(pageParam,query);
	}

	@Override
	public String buildQuerySql(TargetFieldConvertRule dto) {
		StringBuffer sqlbuild = new StringBuffer();
		List<String> queryConditionList = new ArrayList<>();
		
		String tableName ="";
		TargetFieldConvertRule rule = baseMapper.selectById(dto.getId());
		if("codelkup".equals(dto.getType())) {
			tableName = "codelkup";
			queryConditionList.add(" and listname = '" + dto.getListname()+"' ");
		}else if("table".equals(dto.getType())) {
			tableName = dto.getTablename();
		}else{
			throw new RuntimeException("类型不为系统代码，或者table!");
		}
		
		if(StringUtils.hasLength(dto.getExternquerycondition())) {
			queryConditionList.add(dto.getExternquerycondition());
		}
		
		sqlbuild.append("select distinct ");
		sqlbuild.append(dto.getTargetfield()+" as target, ");
		sqlbuild.append(dto.getSourcetablecolumn()+" as source ");
		sqlbuild.append(" from ");
		sqlbuild.append(" "+tableName+" ");
		sqlbuild.append(" where 1=1 ");
		if(StringUtils.hasLength(dto.getExternquerycondition())) {
			sqlbuild.append(" "+ dto.getExternquerycondition());
		}
		if(queryConditionList.size() > 0) {
			for(String query:queryConditionList) {
				sqlbuild.append(query);
			}
		}
		
		return sqlbuild.toString();
	}

	@Override
	public List<Map<String, String>> previewData(TargetFieldConvertRule dto) {
		List<Map<String,String>> list = new ArrayList<>();
		try {
			if("current".equals(dto.getDatasource())) {
				throw new RuntimeException("数据源为动态数据源无法预览"); 
			}
			MultipleDataSource.setDataSourceKey(dto.getDatasource());
			list = baseMapper.queryPreData(dto.getQuerymappingsql());
			MultipleDataSource.ClearDataSourceKey();
		} catch (Exception e) {
			log.error("",e);
			MultipleDataSource.ClearDataSourceKey();
		}
		return list;
	}
	
	
}
